package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.User;
import com.rk.admin.service.UserService;
import com.rk.admin.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "用户管理")
@RestController
@CrossOrigin //前后端分离配置跨域访问
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(("用户登录"))
    @SystemControllerLog(description = "用户登录")
    @PostMapping("/login")
    public CommonResult login(@RequestBody User loguser){
        String username = loguser.getUsername();
        String password = loguser.getPassword();
        //将加密后的密码传入
        UsernamePasswordToken token=new UsernamePasswordToken(username,//密码进行加密:规则 用户名+密码 加密100次
                new Md5Hash(password,username+password,100).toString());
        //shiro提供的认证类
        Subject subject= SecurityUtils.getSubject();
        try {
            if(!subject.isAuthenticated()){//用户是否认证
                subject.login(token);
            }
        } catch (UnknownAccountException e) {//用户名错误抛这个异常
            return CommonResult.failed("用户名错误!");//登录失败
        }catch (IncorrectCredentialsException e) {//密码错误抛这个异常
            return CommonResult.failed("密码错误!");
        }
        //通过验证，登录成功返回 User
        User  user= userService.login(loguser.getUsername());
        return CommonResult.success(user);
    }

    @ApiOperation("用户注销")
    @SystemControllerLog(description = "用户注销")
    @GetMapping("/logout")
    public CommonResult logout(){
        SecurityUtils.getSubject().logout();
        System.out.println("用户退出===========================================");
        return CommonResult.success(200);
    }

    @ApiOperation("添加用户")
    @SystemControllerLog(description = "添加用户")
    @PostMapping("/add")
    public CommonResult UserAdd(@RequestBody User user){
        Boolean flag = userService.UserAdd(user);
        if(flag){//添加成功
            return CommonResult.success("添加成功!");
        }else {
            return CommonResult.failed("添加失败!");
        }
    }


    @ApiOperation("修改用户")
    @SystemControllerLog(description = "修改用户")
    @PostMapping("/updatebyuid")
    public CommonResult updatebyuid(@RequestBody User user){
        Boolean flag = userService.updatebyuid(user);
        if(flag){//添加成功
            return CommonResult.success("修改用户成功!!");
        }else {
            return CommonResult.failed("修改用户失败!");
        }
    }


    @ApiOperation("根据uid删除用户")
    @SystemControllerLog(description = "删除用户")
    @PostMapping("/deletebyuid")
    public CommonResult Userdelete(@RequestParam("uid") int uid){
        Boolean flag = userService.deletebyuid(uid);
        if(flag){
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed();
        }

    }


    @ApiOperation("根据uid查找用户")
    @PostMapping("/findbyuid")
    public CommonResult findbyuid(@RequestParam("uid") int uid){
        return CommonResult.success(userService.findbyuid(uid));
    }


    @ApiOperation(("分页查询用户列表"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始页码",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页数据条数",required = true,example = "5"),
            @ApiImplicitParam(name = "username",value = "用户名",example = "admin"),
            @ApiImplicitParam(name = "roles",value = "当前登录人权限",example = "1"),
    })
    @GetMapping("/findPage")
    public CommonResult findPage(Integer page,Integer limit,String username,String roles){
        return CommonResult.success(userService.selectPage(page,limit,username,roles));
    }


}
